package Server.Poker.NTPDK;

import java.util.HashMap;
import java.util.Map;
import net.sf.json.*;
import Server.Agent.GameAgent;
import Server.Model.Base.IRoom;
import Server.Model.Base.SHMUser;
import Server.Model.Hall.QpGameMgr;
import Server.Model.Hall.dthall;
import common.routine.GlobalSetting;

public class PDKTableMgr {
	private static final PDKTableMgr INSTANCE = new PDKTableMgr();

	public static final PDKTableMgr getInstance() {
		return INSTANCE;
	}
	long m_LastUpdateTime;

	public PDKTableMgr() {
	}

	boolean init() {
		m_LastUpdateTime = 0;

		return true;
	}

	boolean OnClientClose(int lCid, int lUid) {
		onLeaveRoom(lUid);
		return false;
	}

	static long lastDealTime = 0;

	public void OnDeal(int dt) {

		long curTickTime = Constants.getTickCount();
		if (lastDealTime == 0) {
			lastDealTime = curTickTime;
		}
		if (curTickTime - lastDealTime < 180) {
			return;
		} else {
			lastDealTime = curTickTime;
		}
		ProcessRecvdPacket(4096);


	}

	private void ProcessRecvdPacket(int i) {

	}

	public boolean OnRecv(int lCid, int lUid, JSONObject root) {

		String command = root.optString("command");
		String data = root.optString("data");
		int uid = root.optInt("uid");

		if ("ntpdkmsg".equals(command)) {
			onDeal(lCid, lUid, data);
		}

		return true;
	}




	SHMUser getUserInfo(int uid) {
		return Server.Model.Hall.dthall.getInstance().getUserInfo(uid);
	}

	static PDKZTable FindTable(int _Uid) {
		PDKZTable it = (PDKZTable) QpGameMgr.getInstance().getUserRoomByUid(_Uid);
		if (it != null) {
			return it;
		}
		return null;
	}

	void OnUserStandUp(int lUid) {
		Integer _Uid = lUid;
		PDKZTable pTable = (PDKZTable) FindTable(_Uid);

		
		if (pTable != null) {
			pTable.OnUserStandUp(_Uid, true);
			GameAgent agent = GameAgent.findAgentByUid(lUid);
			QpGameMgr.getInstance().Handle_OnUserRoom(agent, new JSONObject());

		}
	}

	void onEnterRoom(int lUid, String[] vecArg) {

//		int multiple = Constants.atoi(vecArg[3]);
//
//		SHMUser user = null;
//		if ((user = getUserInfo(lUid)) != null) {
//			if (multiple <= 0) {
//				multiple = 1;
//			} else if (multiple > 100000) {
//				multiple = 100;
//			}
//			DDZTable tbl = match_table(lUid);
//			if (tbl != null) {
////				m_mapPlayerTable.put(lUid, tbl);
//				tbl.setAppHandler(this);
//				if (tbl.hasUser(lUid) == false) {
////					tbl.Init();
//					//tbl.OnUserSit(user);
//				} else {
////					tbl.UnInit();
////					tbl.Init();
//				}
//			}
//		}
	}

	void Handle_onStart(int lUid, String[] vecArg) {
		if (vecArg.length != 2)
			return;
		SHMUser user = null;
		user = getUserInfo(lUid);
		PDKZTable tbl = match_table(lUid);
		if (tbl != null) {
//			m_mapPlayerTable.put(lUid, tbl);
			tbl.setAppHandler(this);
			if (tbl.hasUser(lUid) == false) {
				//tbl.OnUserSit(user);
			}
			tbl.StartGame(lUid);
		}
	}

	void onLeaveRoom(int lUid) {
		OnUserStandUp(lUid);
	}

	void onPlayAction(int uid, String[] vecArg) {
		PDKZTable iter;
		if (QpGameMgr.getInstance().isUserInRoom(uid)) {
			iter = (PDKZTable) QpGameMgr.getInstance().getUserRoomByUid(uid);
			if (iter != null) {
				iter.OnRecv(uid, vecArg);
			}
		}
	}

	void onDeal(int lCid, int lUid, String sData) {

		String[] vecArg2 = sData.split("\\|");
		
		String subcmd = vecArg2[0];
		
		IRoom  iterUser =QpGameMgr.getInstance().getUserRoomByUid(lUid);
		if (iterUser == null)
		{
			return;
		}
		
		PDKZTable tbl =(PDKZTable) iterUser;
		

		SHMUser shmUser = this.getUserInfo(lUid);
		if (shmUser == null) {
			return;
		}
		
		if (vecArg2.length > 0) {
			if (vecArg2[0].equals("start")) {
				Handle_onStart(lUid, vecArg2);
			} else if (vecArg2[0].equals("leave")) {
				OnUserStandUp(lUid);
			} else if (vecArg2[0].equals("heart")) {

			} else if (vecArg2[0].equals("join")) {
				onEnterRoom(lUid, vecArg2);
			} else if (subcmd.equals("biaoqing")) {
				tbl.Handle_onBiaoQing(lUid, vecArg2);
			}else if (subcmd.equals("yuyin")) {
				tbl.Handle_onYuYin(lUid, vecArg2);
			}
			else if (subcmd.equals("chat")) {
				tbl.Handle_onChat(lUid, vecArg2);
			}
			else if (subcmd.equals("voice")) {
				tbl.Handle_onVoice(lUid, vecArg2);
			} else if(subcmd.equals("expr")){
				tbl.Handle_onExpr(lUid, vecArg2);
			} else if (subcmd.equals("voicemsg")) {
				tbl.Handle_onVoiceMsg(lUid, vecArg2);
			}else if (subcmd.equals("text")) {
				tbl.Handle_onText(lUid, vecArg2);
			}else if (subcmd.equals("try_dismissroom")) {
				tbl.Handle_onTryDismissRoom(lUid, vecArg2);
			}
			else if (subcmd.equals("agree_dismissroom")) {
				tbl.Handle_onAgreeDismissRoom(lUid, vecArg2);
			}else if (subcmd.equals("inBack")){
				tbl.Handle_InBack(lUid,vecArg2);
			} else if (subcmd.equals("inFore")){
				tbl.Handle_InFore(lUid,vecArg2);
			}else if(subcmd.equals("gameiap")){
				tbl.Handle_GameIap(lUid, vecArg2);
			}
			else if (subcmd.equals("refuse_dismissroom")) {
				tbl.Handle_onRefuseDismissRoom(lUid, vecArg2);
			}else {
	
				int m_wMsgType = Constants.atoi(vecArg2[0]);
				switch (m_wMsgType) {
//				case Constants.PktID_Login:// 登陆
//					onLogin(lUid, vecArg2);
//					break;
				case Constants.PktID_UserEnterRoom:// 进入房间
					onEnterRoom(lUid, vecArg2);
					break;
				case Constants.PktID_DDZ_TableData:// 牌局消息
					onPlayAction(lUid, vecArg2);
					break;
				case Constants.PktID_UserLeaveRoom:// 离开房间
					onLeaveRoom(lUid);
					break;
				default:
					break;
				}
			}
		}
	}

	PDKZTable match_table(int lUid) {
		if (QpGameMgr.getInstance().isUserInRoom(lUid)) {
			return (PDKZTable) QpGameMgr.getInstance().getUserRoomByUid(lUid);
		}
		return null;
	}

	PDKZTable create_table(int id) {

		PDKZTable ptbl = new PDKZTable();
		ptbl.SetTableID(id);
//		m_mapTables.put(id, ptbl);
		ptbl.setAppHandler(this);
//		ptbl.Init();
		ptbl.setBase(1);
		return ptbl;
	}

	void onSend(int lUid, String data) {
		onSend(lUid, data, 0);
	}

	void onSend(int lUid, String sData, int iSize) {
		String sDataBuffer = sData;
		sDataBuffer = "{\"command\":\"ntpdkmsg\"" + sDataBuffer + "}";
		Server.Model.Hall.dthall.getInstance().onSend(lUid, sDataBuffer);
	}

	public PDKZTable CreateTable(int _Uid, String strParam, boolean isFangGuan) {
		try {
			int byCostGem = 1;
			SHMUser user = dthall.getInstance().getUserInfo(_Uid);
			if (user == null)
				return null;
			if (strParam == null)
				return null;
			String[] vecParam = strParam.split(";");
			if (vecParam.length == 0)
				return null;

			String roomType = "ZUJU";
			String payway = "AAPAY";
			
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];
				if (param.equalsIgnoreCase("FZPAY")) {
					payway = "FZPAY";
				}
				if (param.equalsIgnoreCase("AAPAY")) {
					payway = "AAPAY";
				}
				if(param.equalsIgnoreCase("CLUBPAY")){
					payway = "CLUBPAY";
				}
			}
			
			
			
			int jushu = 5;
			int paiMode = 0;
			int jiesuanMode = 0;
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];

				if (param.equalsIgnoreCase("5JU")) {
					jushu = 5;
					byCostGem = 5;
				}
				if (param.equalsIgnoreCase("10JU")) {
					jushu = 10;
					byCostGem = 10;
				}
				if (param.equalsIgnoreCase("15JU")) {
					jushu = 15;
					byCostGem = 15;
				}
				if(param.equalsIgnoreCase("88JU")){
					jushu = 88;//一定是个gold模式
				}
				if (param.equalsIgnoreCase("YINGJIA")) {
					jiesuanMode = 0;
				}
				if (param.equalsIgnoreCase("LIANG")) {
					jiesuanMode = 1;
				}
				if (param.equalsIgnoreCase("123A")) {
					paiMode = 0;
				}
				if (param.equalsIgnoreCase("4A")) {
					paiMode = 1;
				}
				if(param.equalsIgnoreCase("DIZHU")){
					roomType = "GOLD";
				}
			}
			
			int maxCost = byCostGem;
			if(payway.equalsIgnoreCase("CLUBPAY")){
				maxCost = 0;
			}
			int dizhu = 0;
			int ruchang = 0;
			int lichang = 0;
			if(roomType.equalsIgnoreCase("GOLD")){
				maxCost = 0;
				byCostGem = 1;
				payway = "GOLD";
				dizhu = Integer.parseInt(vecParam[vecParam.length - 3]);
				ruchang = Integer.parseInt(vecParam[vecParam.length - 2]);
				lichang = Integer.parseInt(vecParam[vecParam.length - 1]);
			}
			if (jushu > 0 && byCostGem > 0 && user.getGem(0) >= maxCost) {
				int factor = 0;
				for (int i = 0; i < 1000000; i++) {
					int rand_room = GlobalSetting.getInstance().getRandRoomNum();//Constants.rand(100000, 999999, factor);
					factor = rand_room;
					IRoom room1 = QpGameMgr.getInstance().getRoomById(rand_room);
					if (room1 == null) {
						PDKZTable room = create_table(rand_room);
						room.m_dwTableID = rand_room;
						room.m_byCostGem = byCostGem;
						room.m_payWay = payway;
						room.m_byMaxRound = jushu;
						room.m_Creator = isFangGuan ? _Uid : 0;
						room.m_roomParam = strParam;
						room.m_paiMode = paiMode;
						room.m_jiesuanMode = jiesuanMode;
						QpGameMgr.getInstance().putRoomIntoMap(rand_room, room);
						if(roomType.equalsIgnoreCase("GOLD")){
							//设置参数
							room.setBeiShu(dizhu, ruchang, lichang);
							room.setRoomType(roomType);
						}else if(roomType.equalsIgnoreCase("DWC")){
							//设置参数
						}
						return room;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

}
